
package com.scs.application.modules.upms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.upms.entity.DictData;
import com.scs.application.modules.upms.entity.DictType;
import com.scs.application.modules.upms.mapper.DictTypeMapper;
import com.scs.application.modules.upms.request.DictTypeQueryRequest;
import com.scs.application.modules.upms.service.DictDataService;
import com.scs.application.modules.upms.service.DictTypeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 字典类型 service 实现
 *
 */
@Slf4j
@Service
@AllArgsConstructor
public class DictTypeServiceImpl extends BaseServiceImpl<DictTypeMapper, DictType> implements DictTypeService {

    private final DictDataService dictDataService;


    @Override
    public boolean removeCascadeById(Serializable id) {
        dictDataService.remove(Wrappers.<DictData>query().eq("dict_type_id", id));
        return super.removeById(id);
    }

    @Override
    public List<DictType> list(DictTypeQueryRequest request) {
        List<DictType> list = this.list(Wrappers.<DictType>query()
                .like(StringUtils.isNotBlank(request.getCode()), "code", request.getCode())
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName())
                .eq(StringUtils.isNotBlank(request.getType()), "type", request.getType())
                .orderByDesc("gmt_modified")
        );
        return list;
    }

    @Override
    public IPage<DictType> page(DictTypeQueryRequest request) {
        IPage page = this.page(request.getPage(), Wrappers.<DictType>query()
                .like(StringUtils.isNotBlank(request.getCode()), "code", request.getCode())
                .like(StringUtils.isNotBlank(request.getName()), "name", request.getName())
                .eq(StringUtils.isNotBlank(request.getType()), "type", request.getType())
                .orderByDesc("gmt_modified")
        );
        return page;
    }


}
